import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import "../component" as C
import "qrc:/component/ZGlobal.js" as Global

C.ZModelRect {
    id: rootModifyPasswordPage
    x:0
    y:0
    width: 800
    height: 656
    anchors.centerIn: parent
    color: "#F3F7FB"
    radius: 10
    visible: false
    // 错误提示与类型（用于高亮相应输入框）
    property string errorMessage: ""
    property string errorType: "" // "account" 或 "password" 或 ""
    // 清空输入与错误状态（在打开弹窗前调用）
    function resetForm() {
        errorMessage = ""
        errorType = ""
        originPassword.text = ""
        newPassword.text = ""
        confirmPassword.text=""
    }

    Image {
        x: 40
        y: 40
        source: "qrc:/assets/greenrect.png"
        width: 16
        height: 28
        fillMode: Image.PreserveAspectFit
    }

    Label{
        x:68
        y:36
        Text{
            text: "密码修改"
            font.pixelSize: 28
            color: "#333333"
            font.bold: true
        }
    }

    Label{
        x:40
        y:108
        Text{
            text: "原密码"
            font.pixelSize: 24
            color: "#333333"
        }
    }


    C.ZInputImage{
        id: originPassword
        x:40
        y:151
        echoMode: TextInput.Password
        placeholderText: "请输入原密码"
        onTextChanged: {
            console.log("text:>>>", text);
        }
    }

    Label{
        x:40
        y:252
        Text{
            text: "新密码"
            font.pixelSize: 24
            color: "#333333"
        }
    }

    C.ZInputImage{
        id: newPassword
        x:40
        y:295
        echoMode: TextInput.Password
        placeholderText: "请输入新密码"
        onTextChanged: {
            console.log("text:>>>", text);
        }
    }

    Label{
        x:40
        y:396
        Text{
            text: "确认新密码"
            font.pixelSize: 24
            color: "#333333"
        }
    }

    C.ZInputImage{
        id: confirmPassword
        x:40
        y:439
        echoMode: TextInput.Password
        placeholderText: "请再次输入新密码"
        onTextChanged: {
            console.log("text:>>>", text);
        }
    }
    Label{
        x:40
        y:511

        Text{
            id: errInfo
            text: "请输入正确的注册码"
            font.pixelSize: 20
            color: "red"
            visible: false
        }
    }

    C.ZButton{
        x: 328
        y: 556
        text: "取消"
        onClicked: {
            // console.log(">>>>>>>>>>>>")
            rootModifyPasswordPage.visible=false
            if (userInfo.isFirstLogin()){
                loginPage.item.resetForm()
                loginPage.item.visible = true
            }
        }
    }


    C.ZButton{
        x: 560
        y: 556
        text: "确定"
        color: "#178FCD"
        fontColor: "white"
        onClicked: {
            console.log("text:>>",originPassword.text.length)
            errInfo.text="";
            do{
                if (originPassword.text.length === 0){
                    errInfo.text = "请输入原密码"
                    break;
                }
                if(originPassword.text !== userInfo.currentLogin.password){
                    errInfo.text = "原密码校验不通过"
                    break;
                }

                if (newPassword.text.length === 0){
                    errInfo.text = "请输入新密码"
                    break;
                }

                if (newPassword.text === originPassword.text){
                    errInfo.text = "新密码不能与原密码相同"
                    break;
                }

                if (newPassword.text !== confirmPassword.text){
                    errInfo.text = "新密码两次输入不一致"
                    break;
                }

                var passwordComplexity = settingInfo.getSettingParam("login_passwordComplexity")
                var passwordLength = settingInfo.getSettingParam("login_passwordLength")
                console.log(passwordComplexity, passwordLength)
                errInfo.text = Global.validatePassword(passwordComplexity, passwordLength, newPassword.text)
                if (errInfo.text.length >0 )break;

                if (userInfo.hasBeenUsed(3, newPassword.text)){
                    errInfo.text = "新密码不能与最近三次的密码相同"
                    break;
                }

            }while(0);
            var passwordLockAttempts = settingInfo.getSettingParam("login_passwordLockAttempts")

            if (errInfo.text.length > 0){
                errInfo.visible = true;
                return;
            }
            userInfo.modifyPassword(newPassword.text)
            rootModifyPasswordPage.visible=false
        }
    }
}


